<template>
  <div>
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0">
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <el-table
          border
          style="margin: 10px 0"
          :data="records"
          show-overflow-tooltip
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description"></el-table-column>
          <el-table-column label="SPU操作">
            <template #="{ row }">
              <el-button
                type="primary"
                size="small"
                title="添加SKU"
                icon="Plus"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                title="编辑SPU"
                icon="Edit"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                title="查看SKU"
                icon="View"
                @click="getSkuList(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                title="删除SKU"
                icon="Delete"
                @click="deleteSpu(row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout=",prev, pager, next, jumper,->,total, sizes"
          :total="total"
          @current-change="getSpu"
          @size-change="changeSize"
        />
      </div>
      <spuForm
        ref="spu"
        v-show="scene == 1"
        v-model:scene="scene"
        @update:scene="changeScene"
      ></spuForm>
      <skuForm
        ref="sku"
        v-show="scene == 2"
        v-model:scene="scene"
        @update:scene="changeScene"
      ></skuForm>
    </el-card>
    <el-dialog v-model="showSku" title="SKU列表">
      <el-table :data="skuList" border>
        <el-table-column label="SKU名称" prop="skuName"></el-table-column>
        <el-table-column label="SKU价格" prop="price"></el-table-column>
        <el-table-column label="SKU重量" prop="weight"></el-table-column>
        <el-table-column label="SKU图片">
          <template #="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showSku = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from 'vue'
import useCategoryStore from '@/store/modules/category'
import { reqHasSpu, reqDeleteSpu } from '@/api/product/spu'
import { reqGetSkuList } from '@/api/product/sku'
import type {
  HasSpuResponseData,
  Records,
  SpuData,
} from '@/api/product/spu/type'
import skuForm from './skuForm.vue'
import spuForm from './spuForm.vue'
import { ElMessage } from 'element-plus'
let scene = ref<number>(0)
let pageNo = ref<number>(1)
let pageSize = ref<number>(3)
let total = ref<number>(0)
let categoryStore = useCategoryStore()
let records = ref<Records>([])
const spu = ref<any>()
const sku = ref<any>()
let showSku = ref(false)
let skuList = ref<SpuData[]>([])
watch(
  () => categoryStore.c3Id,
  () => {
    if (categoryStore.c3Id) {
      getSpu()
    }
  },
)
// getSpu的回调是pageNo 当前页码
const getSpu = (pager = 1) => {
  pageNo.value = pager
  reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id).then(
    (res: HasSpuResponseData) => {
      if (res.code == 200) {
        records.value = res.data.records
        total.value = res.data.total
      }
    },
  )
}
const changeSize = () => {
  getSpu()
}
const addSpu = () => {
  scene.value = 1
  spu.value.initAddSpuData(categoryStore.c3Id)
}
const updateSpu = (row: SpuData) => {
  scene.value = 1
  spu.value.initSpuData(row)
}
const changeScene = (obj: any) => {
  scene.value = obj.scene
  if (obj.type == 'update') {
    getSpu(pageNo.value)
  } else {
    getSpu()
  }
}
const deleteSpu = (row: SpuData) => {
  reqDeleteSpu(row.id as number).then((res) => {
    if (res.code == 200) {
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      getSpu(records.value.length == 1 ? pageNo.value - 1 : pageNo.value)
    } else {
      ElMessage({
        type: 'error',
        message: '删除失败',
      })
    }
  })
}
const addSku = (row: SpuData) => {
  scene.value = 2
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
const getSkuList = (row: SpuData) => {
  reqGetSkuList(row.id as number).then((res) => {
    if (res.code == 200) {
      console.log('data', res.data)
      skuList.value = res.data
      showSku.value = true
    }
  })
}
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>
<script lang="ts">
export default {
  name: 'SPU',
}
</script>

<style lang="scss"></style>
